<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/10/31
 * Time: 14:58
 */

namespace Home\Controller;
use Think\Controller;
class CenterController extends Controller {
    public function index()
    {
       $this->checkSession();
        $this->display();
    }
    public function checkSession(){
        $user_nickname = $_SESSION['user_nickname'];
        if(isset($user_nickname)){
            $this->assign('user_nickname',$user_nickname);
        }else{
            $this->error("需先登录,方可进行查询！",U('Login/index'));
        }
    }
    public function user_info()
    {
        $this->checkSession();
        $user_nickname = $_SESSION['user_nickname'];
        $user_id=$_SESSION['user_id'];
        $user=M('user')->where("user_nickname='$user_nickname'")->find();
        $this->assign("user_nickname",$user_nickname);
        $this->assign("user_name",$user["user_name"]);
        $this->assign("user_idcard",$user["user_idcard"]);
        $this->assign("user_mobile",$user["user_mobile"]);
        $this->assign("user_email",$user["user_email"]);
        $this->display();
    }
    public function info(){
        $this->checkSession();
        $user_nickname = $_SESSION['user_nickname'];
        $user=M('user')->where("user_nickname='$user_nickname'")->find();
        $this->assign("user_name",$user["user_name"]);
        $this->display();
    }
    public function pass()//修改密码
    {
        $this->checkSession();
        $user_nickname = $_SESSION['user_nickname'];
        $this->assign("user_nickname",$user_nickname);
        $this->display();
    }
    public function update_pass(){
        $this->checkSession();
        $user_nickname = $_SESSION['user_nickname'];
        $this->assign("user_nickname",$user_nickname);
        $user=M('user')->where("user_nickname='$user_nickname'")->find();
        $this->assign("user_password",$user['user_password']);
        $password=md5($_POST['old_pass']);
        if($user['user_password']==$password)
        {
            $data['user_password'] = md5($_POST['new_pass1']);
            $condition['user_nickname']=$user_nickname;
            $result=M("user")->where($condition)->data($data)->save();
            if($result !==false) {

                $this->success('修改成功，请记住你的新密码！');
            }else{
                $this->error("异常错误，请稍后重试！");
            }
        }
        else{
            $this->error("你输入的原密码错误！");
        }
    }
    public function user_order()
    {
        $this->checkSession();
        $this->recordList();
        $this->display();
    }
    public function recordList(){//订单记录
        $this->checkSession();
        $user_id=$_SESSION['user_id'];
        $record = M('room_order');
        $total=$record->where("user_id='$user_id'")->count();
        $per = 15;
        import('Org.Util.Page');
        $page = new \Page($total,$per);
        $sql = "select * from hotel_room_order where user_id='$user_id'".$page->limit;
        $query = $record -> query($sql);
        $pageList=$page->fpage();
        $this->assign('param',$query);
        $this->assign('pageList',$pageList);
        $this->assign('num',1);
    }

    public function production_recordList(){//订单记录
        $this->checkSession();
        $user_id=$_SESSION['user_id'];
        $record = M('production_order');
        $total=$record->where("user_id='$user_id'")->count();
        $per = 15;
        import('Org.Util.Page');
        $page = new \Page($total,$per);
        $sql = "select * from hotel_production_order where user_id='$user_id'".$page->limit;
        $query = $record -> query($sql);
        $pageList=$page->fpage();
        $this->assign('param',$query);
        $this->assign('pageList',$pageList);
        $this->assign('num',1);
    }
    public function user_productionorder(){
        $this->checkSession();
        $this->production_recordList();
        $this->display();
    }

    public function cancel_order(){//取消订单
        $this->checkSession();
        $user_id=$_SESSION['user_id'];
        $r_order_id=$_GET['r_order_id'];
        $room_order=M('room_order');
        $status=$room_order->where("r_order_id='$r_order_id'")->field("order_status")->select();
        $cancel_number=$room_order->where("user_id='$user_id' and order_status='取消'")->count();
           $a='取消';
        if($cancel_number>=3){
            $data['user_status']='黑名单';
            $condition['user_id']=$user_id;
            M('user')->where($condition)->data($data)->save();
        }
        $instatus=$room_order->where("r_order_id='$r_order_id'")->field("instatus")->select();
        $starttime=$room_order->where("r_order_id='$r_order_id'")->field("starttime")->select();
        $date_elements = explode("-" ,$starttime[0]{'starttime'});
        $now_time=date("Y-m-d H:m:s");
        $last_time=date("$date_elements[0]-$date_elements[1]-$date_elements[2] 20:00:00");//在线支付订单 取消操作的最后期限
        //dump($last_time);
        if ($status[0]{'order_status'} !== '取消') {
        if($instatus[0]{'instatus'}=='待入住'&&$now_time<=$last_time) {
                $data['order_status'] = $a;
                $data['cancel_time']=$now_time;
                $condition['r_order_id'] = $r_order_id;
                $data['operator'] = $_SESSION['user_id'];
                $result = M('room_order')->where($condition)->data($data)->save();
                if ($result !== false) {
                    $this->success('成功取消订单！');
                } else {
                    $this->error('取消订单失败！');
                }
            } else {
                $this->error('请联系酒店工作人员进行处理！');
            }
        }else{
                 $this->error('你已经取消该订单!');
        }
    }

}